package com.bawei.module_home.adapter

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager

import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.lib_image.GlideApp
import com.bawei.module_home.R
import com.bawei.module_home.model.entity.SimpleTypeEntity


class SimpleTypeAdapter(
    var onItemClick: ((position: Int) -> Unit)? = null,
): RecyclerView.Adapter<SimpleTypeAdapter.SimpleTypeHolder>() {

     val simpleTypeList= mutableListOf<SimpleTypeEntity>()

    fun setSimpleTypeData(data:List<SimpleTypeEntity>){
        simpleTypeList.addAll(data)
        notifyDataSetChanged()
    }
    private var mPosition = 0
    fun setPosition(position: Int) {
        mPosition = position
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleTypeHolder {
        val view = View.inflate(parent.context, R.layout.item_simple_type, null)
        return SimpleTypeHolder(view,onItemClick)
    }

    override fun getItemCount(): Int {
        return simpleTypeList.size
    }

    override fun onBindViewHolder(holder: SimpleTypeHolder, position: Int) {
        holder.textView.text = simpleTypeList[position].typename
        if(mPosition == holder.adapterPosition){
            holder.textView.setTextColor(Color.parseColor("#FF2442"))
        }else{
            holder.textView.setTextColor(Color.parseColor("#4B5563"))
        }
    }
    class SimpleTypeHolder(view: View,var onItemClick: ((position: Int) -> Unit)? = null) : RecyclerView.ViewHolder(view) {
        val textView = view.findViewById<TextView>(R.id.type_name)
        init {
            textView.setOnClickListener {
                onItemClick?.invoke(position)
            }
        }
    }

    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
        recyclerView.addItemDecoration(GridSpaceItemDecoration(
            horizontalSpacing = 0,  // 水平间距（左右）
            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.run {
            setItemViewCacheSize(12)//设置屏幕外ViewHolder缓存数量
            setHasFixedSize(true)//减少固定大小的子组件反复测量
            setRecycledViewPool(RecyclerView.RecycledViewPool().apply { setMaxRecycledViews(0, 10) })
            setViewCacheExtension(VideoItemCache(recyclerView))
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE){
                        GlideApp.with(BaseApplication.instance).resumeRequests()
                    }else{
                        GlideApp.with(BaseApplication.instance).pauseRequests()
                    }
                }
            })
        }
    }

}